<?php

namespace App\Http\Controllers;

use App\CountryModel;
use Illuminate\Http\Request;

class CountryController extends Controller
{
    public function addcountry()
    {
        return view("admin.addcountry");
    }


    public function countrys()
    {
        $countrys = CountryModel::get();
        return view("admin.countrys", compact("countrys"));
    }


    public function updatecountry(Request $request)
    {
        $id=$request->input("id");
        $country=CountryModel::where("id",$id)->first();
return view("admin.addcountry",compact("country"));
    }


    public function deletecountry(Request $request)
    {
        $id = $request->input("id");
        $country = CountryModel::where("id", $id)->delete();
        $result = ["title" => "数据删除成功", "err" => 0];
        $result = json_encode($result);
        return $result;
    }



    public function insertcountry(Request $request)
    {
        $country = $request->input();
//        dd($country);
        $id=$country['id'];
        if ($id==0){
            CountryModel::create($country);
            $result = ["title" => "数据添加成功", "err" => 0];
            $result = json_encode($result);
            return $result;
        }else{
           $ok=CountryModel::where("id",$id)->first();
           $ok->update($country);
            $result = ["title" => "数据修改成功", "err" => 0];
            $result = json_encode($result);
            return $result;
        }




    }
}
